package com.codi.bus.core.dao.impl;

import com.codi.base.dao.BaseDAOImpl;
import com.codi.base.dao.plugin.page.PageView;
import com.codi.bus.core.dao.UserDao;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.domain.ext.UserExt;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("userDao")
public class UserDaoImpl extends BaseDAOImpl<User> implements UserDao {

	@Override
	public User add(User entity) {
		entity.setCreateDate(new Date());
		entity.setIsValid(true);
		this.insert(entity);
		return entity;
	}

	@Override
	public void updateUser(User entity) {
		entity.setLastUpdatedDate(new Date());
		super.update(entity);
	}

	@Override
	public boolean isExistMobile(String mobile) {
		User entity= this.getObject(User.class.getName() + ".selectUserByUserMobile", mobile);
		if(entity==null){
			return false;
		}else{
			return true;
		}
	}

	@Override
	public User getEntity(Long userId) {
		return this.getObject(User.class.getName() + ".selectUserByUserId", userId);
	}

    @Override
    public User getEntityByToken(String token) {
        return this.getObject(User.class.getName() + ".selectUserByToken", token);
    }

	@Override
	public User getEntityByMobile(String mobile) {
		return this.getObject(User.class.getName() + ".selectUserByUserMobile", mobile);
	}

	@Override
	public User getEntityByIDCard(String idCard) {
		return this.getObject(User.class.getName() + ".selectUserByIDCard", idCard);
	}

    @Override
    public List<User> getInviteUserInfos(Integer pageIndex, Integer pageSize, Long userId) {
        PageView pageView = getPageView(pageIndex, pageSize);
        Map<Object, Object> param = new HashMap<>();
        param.put("pageView", pageView);
        param.put("userId", userId);

        List<User> userinfos = this.findList(generateStatement("getInviteUserInfos"), param);

        return userinfos;
    }

    @Override
    public List<User> queryList(Integer pageIndex, Integer pageSize, UserExt user) {
        PageView pageView = getPageView(pageIndex, pageSize);
        Map<Object, Object> param = new HashMap<>();
        param.put("pageView", pageView);
        param.putAll(new BeanMap(user));
        List<User> list = this.findList(generateStatement("queryList"), param);
        return list;
    }

    @Override
    public Integer queryListCount(UserExt user) {
        return this.getSqlSession().selectOne(generateStatement("queryListCount"),user);
    }

    @Override
    public List<User> getAllUser() {
        return this.findList(generateStatement("selectAll"));
    }

    @Override
    public void updateAllUsersSysMessageCounts() {
        super.update(generateStatement("updateAllUsersSysMessageCounts"));
    }

    @Override
    public void updateAllUsersSys() {
        super.update(generateStatement("updateAllUsersSys"));
    }

    @Override
    public void updateChinaTax(Long userId) {
        super.update(generateStatement("updateChinaTax"), userId);
    }

    @Override
	public boolean isExistCard(String idCard) {
		User entity= getEntityByIDCard(idCard);
		if(entity==null){
			return false;
		}else{
			return true;
		}
	}


}
